//
//  WDFSaveData.m
//  Catagory
//
//  Created by mac on 2021/3/15.
//

#import "DFSaveData.h"

@implementation DFSaveData

#pragma mark - 将数据存储到本地
+(void)df_saveLocalData:(id)value forKey:(NSString*)key
{
    //获取路径对象
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"localdata.plist"];
    NSMutableDictionary *dictplist = [[[NSMutableDictionary alloc ] initWithContentsOfFile:plistPath] mutableCopy];
    if (dictplist==nil) {//文件不存在则创建文件
        dictplist = [[NSMutableDictionary alloc ]init];
    }
    //设置属性值
    if (value&&key)
    {
        [dictplist setObject:value forKey:key];
    }
    
    //写入文件
    [dictplist writeToFile:plistPath atomically:YES];
}

#pragma mark - 获取本地已经存储的数据
+(id)df_getLocalData:(NSString*)key
{
    //获取路径对象
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"localdata.plist"];
    NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    NSString *str = nil;
    if(dic!=nil){
        str=[dic objectForKey:key];
    }
    return str;
}

#pragma mark - 根据key删除本地的数据
+(id)df_removeLocalSpecifiedData:(NSString*)key
{
    //获取路径对象
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"localdata.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    
    if(dic!=nil){
        
        NSString *dd = [dic objectForKey:key];
        if (dd)
        {
            [dic removeObjectForKey:key];
            
            NSLog(@"===========%@",[dic objectForKey:@"memberId"]);
            [dic writeToFile:plistPath atomically:YES];
            
        }
    }
    return dic;
}

#pragma mark - 删除本地所有的数据
+(id)df_removeLocalData
{
    //获取路径对象
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"localdata.plist"];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    
    if(dic!=nil){
        
        [dic removeAllObjects];
        [dic writeToFile:plistPath atomically:YES];
        
    }
    return dic;
}


#pragma mark - 把对象归档存到沙盒里
+(void)df_saveObject:(id)object byFileName:(NSString*)fileName
{
    NSString *path  = [self appendFilePath:fileName];
    
    [NSKeyedArchiver archiveRootObject:object toFile:path];
    
}

#pragma mark - 通过文件名从沙盒中找到归档的对象
+(id)df_getObjectByFileName:(NSString*)fileName
{
    
    NSString *path  = [self appendFilePath:fileName];
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

#pragma mark - 根据文件名删除沙盒中的 plist 文件
+(void)df_removeFileByFileName:(NSString*)fileName
{
    NSString *path  = [self appendFilePath:fileName];
    
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

#pragma mark - 拼接文件路径
+ (NSString*)appendFilePath:(NSString*)fileName
{
    
    NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    NSString *file = [NSString stringWithFormat:@"%@/%@.archiver",documentsPath,fileName];
    
    return file;
}



@end
